package com.test.exam.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.test.exam.domain.Course;
import com.test.exam.service.BanService;
import com.test.exam.service.CourseService;
import com.test.exam.service.SubjectService;

@Controller
public class CourseController {
	@Autowired
	private CourseService courseService;
	@Autowired
	private BanService banService;
	@Autowired
	private SubjectService subjectService;
	
	
	//페이지 코스 목록
	@RequestMapping(value="/admin/course/getCoursesByCurrentPage/{currentPage}", 
					method=RequestMethod.GET)
	public String getCoursesByCurrentPage(
			@PathVariable int currentPage,
			Model model){
		model.addAttribute(
				"courseList", 
				courseService.getCoursesByCurrentPage(currentPage));
		
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("endPage", courseService.getEndPage());
		
		return "/admin/course/courseList";
	}
	
	
	@RequestMapping(value="/admin/course/addCourse", method=RequestMethod.POST)
	public String addCourse(Course course){
		courseService.addCourse(course);
		return "redirect:/admin/course/getCoursesByCurrentPage/1";
	}
	
	
	
	@RequestMapping(value="/admin/course/addCourseForm", method=RequestMethod.GET)
	public String addCourseForm(Model model){
		model.addAttribute("banList", banService.getAllBans());
		model.addAttribute("subjectList", subjectService.getAllSubjects());
		return "/admin/course/addCourseForm";
	}
}
